import xlrd
import os
import sys
import utils
import buildConfig
CONST_TYPEFILEPATH = "./templates/ts/type.ts"
CONST_CSVFILEPATH = "./templates/ts/csv.ts"
# 导出class描述文件
def writeTypeFile(filePath):
    classname = utils.getFileName(filePath).capitalize()
    print("filename = ",classname)
    excelFile = xlrd.open_workbook(filePath)
    sheet:xlrd.sheet.Sheet= excelFile.sheet_by_index(0)
    yNum = sheet.nrows
    
    curY = 0
    valueNames = []
    valueTypes = []
    typeContent = ""
    configContent = ""
    while curY <= yNum:
        valueName = str(sheet.cell_value(0,curY))
        valueType = str(sheet.cell_value(1,curY))
        valueNames.append(valueName)
        valueTypes.append(valueType)
        configContent += "                "
        if valueType == "string":
            typeContent += "    " + "{} : string = '';\n".format(valueName);
            
        elif  valueType == "number":
            typeContent += "    {} : number = 0;\n".format(valueName);
        elif  valueType == "iarray":
            typeContent += "    {} : number[] = [];\n".format(valueName);
        elif  valueType == "sarray":
            typeContent += "    {} : string[] = [];\n".format(valueName);
        configContent += "{} : params[{}],\n".format(valueName,curY)    
        curY += 1
    tsContent = ""
    templateFile = open(CONST_TYPEFILEPATH,"r",encoding="utf-8")
    tsContent = templateFile.read()
    templateFile.close()
    tsContent = tsContent.replace("#class",classname)
    tsContent = tsContent.replace("//typeContent",typeContent)
    tsContent = tsContent.replace("//configContent",configContent)
    outputFilePath = "../config/outpus/ts/Cfg_{}.ts".format(classname)
    utils.writeFile(outputFilePath,tsContent)

def WriteCsv(fileList):
    tsContent = ""
    templateFile = open(CONST_CSVFILEPATH,"r",encoding="utf-8")
    tsContent = templateFile.read()
    templateFile.close()
    importStrs = ""
    localCsvStrs = ""
    index = 0
    print(fileList)
    for filename in fileList:
        classname = filename.capitalize()
        if index < len(fileList) -1:
            importStrs += 'import Config{} from "./Cfg_{}";\n'.format(classname,classname)
            localCsvStrs += "Config{}.loadCsv();\n".format(classname)
        else:
            importStrs += 'import Config{} from "./Cfg_{}";'.format(classname,classname)
            localCsvStrs += "Config{}.loadCsv();".format(classname)
    tsContent = tsContent.replace("//csvcontenttitle",importStrs)
    tsContent = tsContent.replace("//csvloadall",localCsvStrs)
    outputFilePath = "../config/outpus/ts/Csv.ts"
    utils.writeFile(outputFilePath,tsContent)